﻿/**
 * @file setting_bluetooth_service.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_BLUETOOTH != 0

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/

void watch_bt_switch_message_handle(void * msg)
{
    mmi_msg_bt_switch_response_t * res_msg = (mmi_msg_bt_switch_response_t *)msg;
    printf("bt+++ watch_bt_switch_message_handle result = %d\n", res_msg->result);
    lv_bluetooth_obj_ext_t * ext = bluetooth_get_ext();
    if(MMI_BT_SWITCH_ON_CNF == res_msg->header.MsgId) {
        if(1 == res_msg->result) {
            bluetooth_show_object(true);
            bluetooth_nvm_save_switch_state(true);
        } else {
            if(ext) lv_label_set_text_id(ext->opening_label, WATCH_TEXT_ID_OPEN_FAILED);
        }
    } else if(MMI_BT_SWITCH_OFF_CNF == res_msg->header.MsgId) {
        if(ext) {
            lv_obj_set_hidden(ext->opening_label, true);
            lv_switch_off(ext->sw, LV_ANIM_ON);
        }
        if(watch_is_ready())
            bluetooth_nvm_save_switch_state(false);
        bt_list_destroy();
        bt_adp_task_destroy();
    }
}

void watch_bt_inquiry_message_handle(void * msg)
{
    mmi_msg_bt_inquiry_response_t * res_msg = (mmi_msg_bt_inquiry_response_t *)msg;
    printf("bt+++ watch_bt_inquiry_message_handle complete = %d\n", res_msg->complete);
    if(1 == res_msg->complete) {
        bluetooth_stop_scaning_anim();
    } else {
        bluetooth_update_scan_label_show();
    }
}

void watch_bt_paired_message_handle(void * msg)
{
    mmi_msg_bt_pair_response_t * res_msg = (mmi_msg_bt_pair_response_t *)msg;
    printf("bt+++ watch_bt_paired_message_handle: %d\n", res_msg->paired);
    if(res_msg->paired == 0)
        bluetooth_paired_confirm((char *)res_msg->name, (char *)res_msg->addr);
    else
        bluetooth_update_display();
}

void watch_bt_connect_message_handle(void * msg)
{
    mmi_msg_bt_connect_response_t * res_msg = (mmi_msg_bt_connect_response_t *)msg;
    printf("bt+++ watch_bt_connect_message_handle: %d for_audio: %d\n", res_msg->connect, res_msg->for_audio);
    if(bluetooth_setting_is_active())
        bluetooth_update_display();
    #if USE_LV_WATCH_AUDIO_PLAYER != 0
    if(res_msg->for_audio)
        audio_player_handle_bt_handset_connect_cnf(res_msg->connect);
    #endif
    if(res_msg->connect == 0) {
        Hal_Switch_Audio_Speaker(query_current_volume());
    } else {
        voip_bt_connected();
    }
}

void watch_bt_acl_connect_message_handle(void * msg)
{
    mmi_msg_bt_acl_response_t * res_msg = (mmi_msg_bt_acl_response_t *)msg;
    printf("bt+++ watch_bt_acl_connect_message_handle %d\n", res_msg->connect);
    if(res_msg->connect == 0 ){
        if(res_msg->reason == BT_ERR_CODE_CONNECTION_TERMINATED_BY_LOCAL_HOST || res_msg->reason == BT_ERR_CODE_REMOTE_USER_TERMINATED_CONNECTION ) {
            if(bluetooth_setting_is_active())
                bluetooth_update_display();
        }
        bluetooth_transfer_mbox_update();
        bluetooth_paired_processing_mbox_update();
    } else {
        bluetooth_processing_mbox_update();
    }
}

void watch_bt_obex_message_handle(void * msg)
{
    mmi_msg_bt_obex_t * info = (mmi_msg_bt_obex_t *)msg;
    switch(info->header.MsgId) {
        case MMI_BT_OBEX_FILE_RECEIVE_START:
            bluetooth_receive_file_confirm(0);
            break;
        case MMI_BT_OBEX_FILE_RECEIVING:
            bluetooth_transfer_file_process_update(PROCESS_RECV_FILE);
            break;
        case MMI_BT_OBEX_FILE_RECEIVE_FINISH:
            bluetooth_receive_file_process_update_and_finish();
            break;
        case MMI_BT_OBEX_FILE_RECEIVE_ABORT:
            if(BT_RECEIVE_NO_SDCARD == info->data || BT_RECEIVE_NO_SPACE == info->data
                || BT_RECEIVE_SD_USING == info->data)
                bluetooth_receive_file_abort_tip(info->data);
            else
                bluetooth_transfer_file_finish(PROCESS_RECV_FILE, 0);
            break;
        case MMI_BT_OBEX_FILE_RECEIVE_START_AND_FINISH:
            bluetooth_receive_file_confirm(1);
            break;
        case MMI_BT_OBEX_FILE_SEND_FAILED:
            bluetooth_transfer_file_finish(PROCESS_SEND_FILE, 0);
            break;
        case MMI_BT_OBEX_FILE_SEND_SUCCESS:
            bluetooth_transfer_file_finish(PROCESS_SEND_FILE, 1);
            break;
        case MMI_BT_OBEX_FILE_SEND_START:
            bluetooth_send_file_start();
            break;
        case MMI_BT_OBEX_FILE_SEND_CONTINUE:
            bluetooth_transfer_file_process_update(PROCESS_SEND_FILE);
            break;
        default:
            break;
    }
}
#endif /*USE_LV_BLUETOOTH*/
